<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        background-color: orange;
        position: absolute;
        left: 0;
        top: 0;
      }
    </style>
  </head>
  <body>
    <div id="demo"></div>
    <script>
      const demoEl = document.querySelector("#demo");

      demoEl.onmousedown = function (e) {
        const event = window.event || e;
        let lastX = 0;
        let lastY = 0;
        let iSpeedX = 0;
        let iSpeedY = 0;
        const disX = event.clientX - this.offsetLeft;
        const disY = event.clientY - this.offsetTop;
        const self = this;
        document.onmousemove = function (e) {
          clearInterval(self.timer);
          const event = window.event || e;
          const newLeft = event.clientX - disX;
          const newTop = event.clientY - disY;
          iSpeedX = newLeft - lastX;
          iSpeedY = newTop - lastY;
          lastX = newLeft;
          lastY = newTop;

          const oSpan = document.createElement("span");
          oSpan.style.position = "absolute";
          oSpan.style.left = newLeft + "px";
          oSpan.style.top = newTop + "px";
          oSpan.style.width = "5px";
          oSpan.style.height = "5px";
          oSpan.style.backgroundColor = "black";
          document.body.appendChild(oSpan);

          self.style.left = newLeft + "px";
          self.style.top = newTop + "px";
        };

        document.onmouseup = function () {
          document.onmouseup = null;
          document.onmousemove = null;
          startMove(self, iSpeedX, iSpeedY);
        };
      };

      function startMove(domEl, iSpeedX, iSpeedY) {
        clearInterval(domEl.timer);
        const u = 0.8;
        const g = 3;
        domEl.timer = setInterval(() => {
          iSpeedY += g;
          let newTop = domEl.offsetTop + iSpeedY;
          let newLeft = domEl.offsetLeft + iSpeedX;
          let maxTop =
            document.documentElement.clientHeight - domEl.clientHeight;
          let maxLeft =
            document.documentElement.clientWidth - demoEl.clientWidth;
          if (newTop >= maxTop) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedY *= -1;
            newTop = maxTop;
          }

          if (newTop <= 0) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedY *= -1;
            newTop = 0;
          }

          if (newLeft >= maxLeft) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedX *= -1;
            newLeft = maxLeft;
          }

          if (newLeft <= 0) {
            iSpeedY *= u;
            iSpeedX *= u;
            iSpeedX *= -1;
            newLeft = 0;
          }

          if (Math.abs(iSpeedX) <= 1) {
            iSpeedX = 0;
          }

          if (Math.abs(iSpeedY) <= 1) {
            iSpeedY = 1;
          }

          if (
            iSpeedX === 0 &&
            iSpeedY === 0 &&
            newTop === maxTop &&
            newLeft === maxLeft
          ) {
            clearInterval(domEl.timer);
          } else {
            domEl.style.top = newTop + "px";
            domEl.style.left = newLeft + "px";
          }
        }, 30);
      }
    </script>
  </body>
</html>
